package com.lcvc.xgy.springboot.service;

import com.lcvc.xgy.springboot.dao.AdminDao;
import com.lcvc.xgy.springboot.model.Admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service//使spring扫描器自动进行依赖注入
public class Adminbean {

    @Autowired
    private AdminDao adminDao;

    //管理员登录方法
    public boolean login(String username, String passsword) {
        boolean result = false;
        if (adminDao.login(username, passsword) ==1) {
            result = true;
        }
        return result;
    }

    //获取管理员信息
    public Admin getAdmin(String username) {
        return adminDao.getAdminbyUser(username);
    }

    //管理员列表
    public List<Admin> getAllAdmin() {

        return adminDao.getAdmins();


    }

    //删除管理员
    //要删除的id
    //执行删除的id
    public void deleteAdmin(Integer id, Integer adminid) {
        if (id != null) {
            //判断当前登录id不是要删除的id才执行删除
            if (adminid != id.intValue()) {
                //连接数据库
                adminDao.deleteAdmin(id);
            }
        }


    }

    //添加管理员账户
    public void addAdmin(Admin admin) {
        if (admin != null) {
            admin.setPassword("123456");
            adminDao.addAdmin(admin);
        }


    }


    /**
     * 判断账户名是否重名
     */
    public boolean estimateAdmin(String username) {
        boolean result = false;
        if (adminDao.estimateAdmin(username) > 0) {
            return true;
        }
        return result;

    }


    //根据id读取获取对象
    public Admin getAdmin(Integer id) {
        Admin admin = null;
        if (id != null) {
            admin = adminDao.getAdminbyID(id);
        }
        return admin;
    }


    //账号编辑
    public void updataAdmin(Admin admin) {
        adminDao.updataAdmin(admin);
    }

    //判断编辑时账号名是否重名
    public boolean estimateAdminupdata(String username, Integer id) {
        boolean result = false;
        if (adminDao.estimateAdminupdata(username, id) > 0) {
            result = true;
        }

        return result;

    }

    //管理员查询
    //根据查询条件查询
//    public List<Admin> queryAdmin(AdminQuery adminQuery){
//          return adminDao.queryAdmin(adminQuery);
//    }

    //修改密码
    public  void updataPass(Integer id,String newPass){
//        Admin admin=new Admin();
//        admin.setId(id);
//        admin.setPassword(newPass);
//     adminDao.updataAdmin(admin);
        adminDao.updatePassword(id,newPass);
    }
}


